using Microsoft.AspNetCore.SignalR;
using WMS_Goodsinfo.Api.Write.DTOs;

namespace WMS_Goodsinfo.Api.Write.Hubs
{
    /// <summary>
    /// 仪表板实时数据推送Hub
    /// </summary>
    public class DashboardRealtimeHub : Hub
    {
        private readonly ILogger<DashboardRealtimeHub> _logger;

        public DashboardRealtimeHub(ILogger<DashboardRealtimeHub> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 客户端连接时
        /// </summary>
        public override async Task OnConnectedAsync()
        {
            _logger.LogInformation($"客户端连接: {Context.ConnectionId}");
            await base.OnConnectedAsync();
        }

        /// <summary>
        /// 客户端断开连接时
        /// </summary>
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            _logger.LogInformation($"客户端断开连接: {Context.ConnectionId}");
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Dashboard");
            await base.OnDisconnectedAsync(exception);
        }

        /// <summary>
        /// 加入仪表板组
        /// </summary>
        public async Task JoinDashboardGroup()
        {
            await Groups.AddToGroupAsync(Context.ConnectionId, "Dashboard");
            await Clients.Caller.SendAsync("JoinedGroup", "Dashboard");
            _logger.LogInformation($"客户端 {Context.ConnectionId} 加入Dashboard组");
        }

        /// <summary>
        /// 离开仪表板组
        /// </summary>
        public async Task LeaveDashboardGroup()
        {
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "Dashboard");
            await Clients.Caller.SendAsync("LeftGroup", "Dashboard");
            _logger.LogInformation($"客户端 {Context.ConnectionId} 离开Dashboard组");
        }

        /// <summary>
        /// 发送库存占比数据
        /// </summary>
        public async Task SendInventoryProportionData(InventoryProportionData data)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveInventoryProportion", data);
            _logger.LogDebug("推送库存占比数据到Dashboard组");
        }

        /// <summary>
        /// 发送入库统计数据
        /// </summary>
        public async Task SendInboundStatisticsData(InboundStatisticsData data)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveInboundStatistics", data);
            _logger.LogDebug("推送入库统计数据到Dashboard组");
        }

        /// <summary>
        /// 发送出库统计数据
        /// </summary>
        public async Task SendOutboundStatisticsData(OutboundStatisticsData data)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveOutboundStatistics", data);
            _logger.LogDebug("推送出库统计数据到Dashboard组");
        }

        /// <summary>
        /// 发送排行榜数据
        /// </summary>
        public async Task SendRankingData(RankingData data)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveRankingData", data);
            _logger.LogDebug("推送排行榜数据到Dashboard组");
        }

        /// <summary>
        /// 发送系统消息
        /// </summary>
        public async Task SendSystemMessages(List<SystemMessage> messages)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveSystemMessages", messages);
            _logger.LogDebug($"推送{messages.Count}条系统消息到Dashboard组");
        }

        /// <summary>
        /// 发送单条系统消息
        /// </summary>
        public async Task SendSystemMessage(SystemMessage message)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveSystemMessage", message);
            _logger.LogDebug($"推送系统消息: {message.Title}");
        }

        /// <summary>
        /// 发送完整仪表板数据
        /// </summary>
        public async Task SendDashboardData(DashboardData data)
        {
            await Clients.Group("Dashboard").SendAsync("ReceiveDashboardData", data);
            _logger.LogDebug("推送完整仪表板数据到Dashboard组");
        }

        /// <summary>
        /// 客户端请求最新数据
        /// </summary>
        public async Task RequestLatestData()
        {
            await Clients.Caller.SendAsync("DataRequested");
            _logger.LogDebug($"客户端 {Context.ConnectionId} 请求最新数据");
        }

        /// <summary>
        /// 获取在线用户数
        /// </summary>
        public async Task GetOnlineUsers()
        {
            // 这里可以实现获取在线用户数的逻辑
            var onlineCount = 1; // 简化实现
            await Clients.Caller.SendAsync("OnlineUsersCount", onlineCount);
        }
    }
}